package com.lex.crossgate.admin.common.job.controller;

import com.alibaba.fastjson.JSONObject;
import com.lex.crossgate.admin.common.job.entity.QuartzJobEntity;
import com.lex.crossgate.admin.common.job.service.QuartzJobService;
import com.lex.crossgate.admin.common.log.config.Log;
import com.lex.crossgate.admin.common.utils.JsonDataFormate;
import com.lex.crossgate.common.constants.R;;
import com.lex.crossgate.admin.modules.sys.entity.SysUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * TODO: 定时任务
 * @author 李贝留斯
 * @email sunyadongsyd@163.com
 * @date 2018/6/21 15:15
 */
@Controller
@RequestMapping("sys/job")
public class QuartzJobController {

	@Autowired
	private QuartzJobService quartzJobService;

	@GetMapping(value = "/index.html")
	public ModelAndView index(){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("base/job/jobList");
		return modelAndView;
	}

	@RequestMapping(value = "/add.html")
	public String add(){ return "base/job/jobAdd";}



	@RequestMapping(value = "/edit.html")
	public ModelAndView edit(String jobId){
		ModelAndView modelAndView = new ModelAndView();
		QuartzJobEntity quartzJobEntity = quartzJobService.getQuartzJobById(jobId);
		modelAndView.setViewName("base/job/jobEdit");
		modelAndView.addObject("quartzJob",quartzJobEntity);
		return modelAndView;
	}


    @Log("获取定时任务")
    @ResponseBody
    @RequestMapping("/getJob.html")
    public JSONObject getJob(QuartzJobEntity quartzJobEntity,int page,int limit) {
        PageHelper.startPage(page,limit);
        List<QuartzJobEntity> jobList = quartzJobService.getJob(quartzJobEntity);
        PageInfo pageInfo = new PageInfo(jobList);
        JSONObject jsonObject = JsonDataFormate.piecing(jobList,pageInfo);
        return jsonObject;
    }


	@Log("保存定时任务")
    @ResponseBody
    @RequestMapping("/save.html")
	public R save(QuartzJobEntity job) {
		R st = quartzJobService.save(job);
		return st;
	}

	@Log("更新定时任务")
    @ResponseBody
    @RequestMapping("/update.html")
	public R update(QuartzJobEntity job) {
		return quartzJobService.updateQuartzJob(job);
	}

	
	/**
	 * 删除定时任务
	 * @param ids
	 * @return
	 */
	@Log("删除定时任务")
    @ResponseBody
    @RequestMapping("/delete.html")
	public R remove(String[] ids) {
		return quartzJobService.batchRemoveQuartzJob(ids);
	}


	/**
	 * 立即运行
	 * @param id
	 * @return
	 */
	@Log("立即运行定时任务")
    @ResponseBody
    @RequestMapping("/run")
	public R run(String[] id) {
		return quartzJobService.run(id);
	}
	
	/**
	 * 暂停任务
	 * @param id
	 * @return
	 */
	@Log("暂停定时运行")
    @ResponseBody
    @RequestMapping("/disable")
	public R pause(String[] id) {
		return quartzJobService.pause(id);
	}
	
	/**
	 * 启用任务
	 * @param id
	 * @return
	 */
	@Log("启用定时任务")
    @ResponseBody
    @RequestMapping("/enable")
	public R resume(String[] id) {
		return quartzJobService.resume(id);
	}
	
}
